var wxpay = {},
    alpay = {};
var thirdpay = {};
// iswxchoiceflag 为true时，选择的是微信支付
thirdpay.pay = function(data1, iswxchoiceflag) {
  // alert(JSON.stringify(data1))的
  data1['device'] = api.systemType;
  // data1.concat({'device':api.systemType})
  // alert(JSON.stringify(data1))
    new rest().get('alipay').datae(data1).success(function(res) {
        if (res.flag == 1) {
            if (iswxchoiceflag) {
                wxpay.pay(res)

            } else {
                alpay.pay(res)
            }
        } else {
            api.toast({
                msg: res.msg,
                duration: 2000,
                location: 'middle'
            });
        }
        console.log(JSON.stringify(res))
    }).error(function(err) {
        console.log(JSON.stringify(err))
    }).run()

}

//支付宝支付这个需要配置xml文件和ret文件
alpay.pay = function(res) {
        if (res.flag == 1) {
            var aliPayPlus = api.require('aliPayPlus');
            // console.log('ddddddddddddddddddd');
            aliPayPlus.payOrder({
                orderInfo: res.msg
            }, function(ret, err) {
                // console.log(JSON.stringify(ret))
                // console.log('ERR  '+JSON.stringify(err))
                if (ret.code == 9000) {
                    // api.toast({
                    //     msg: '支付成功',
                    //     duration: 2000,
                    //     location: 'middle'
                    // });
                    api.sendEvent({
                        name: 'paysuccess',
                    });
                } else if (ret.code == 6001) {
                    api.toast({
                        msg: '用户中途取消',
                        duration: 2000,
                        location: 'middle'
                    });

                } else if (ret.code == 4000) {
                    api.toast({
                        msg: '订单支付失败',
                        duration: 2000,
                        location: 'middle'
                    });

                } else {
                    api.toast({
                        msg: '支付结果未知',
                        duration: 2000,
                        location: 'middle'
                    });

                }

            });
        }
    }
    //微信支付
wxpay.pay = function(res) {
  var wx = api.require('wx');
  wx.isInstalled(function(ret, err) {
      if (ret.installed) {
        if (res.flag == 1) {
            console.log(JSON.stringify(res) + 'RES')
            prepay_id = res.msg.prepay_id;
            var myDate = new Date();
            var now = Date.parse(new Date()) / 1000;
            var stringA = "appid=wx1a3c93f012dc68dd&noncestr=" + res.msg.nonce_str + "&package=Sign=WXPay&partnerid=1518085441&prepayid=" + prepay_id + "&timestamp=" + now;
            var stringSignTemp = stringA + "&key=cc2c294176d31c2b170051724fa3a957";
            var sign = md5(stringSignTemp).toUpperCase();
            var wxPay = api.require('wxPay');
            wxPay.payOrder({
                'apiKey': 'wx1a3c93f012dc68dd',
                'orderId': prepay_id,
                'mchId': '1518085441',
                'nonceStr': res.msg.nonce_str,
                'timeStamp': now,
                'package': 'Sign=WXPay',
                'sign': sign
            }, function(ret, err) {
                console.log(JSON.stringify(ret))
                if (ret.status) {
                    // api.toast({
                    //     msg: '支付成功',
                    //     duration: 2000,
                    //     location: 'middle'
                    // });
                    api.sendEvent({
                        name: 'paysuccess',
                    });

                    //支付成功
                } else {
                    if (err.code == -2) {
                        api.toast({
                            msg: '用户中途取消',
                            duration: 2000,
                            location: 'middle'
                        });

                    }
                    console.log(err.code);
                }
            });
        }
      }else {
        if (api.systemType == 'ios') {
            api.installApp({
                appUri: 'https://itunes.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124?mt=8' //安装包对应plist地址
            });
        } else {
          api.openWin({
              name: 'down',
              url: 'widget://html/VIP/down.html',
              pageParam: {
                  url: 'https://android.myapp.com/myapp/detail.htm?apkName=com.tencent.mm&ADTAG=mobile'
              }
          });

        }
      }
    })

}
